//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System.Windows.Forms;
using Microsoft.TridentWordAddIn.Common;
using System.Linq;
using System.Collections.Generic;
using Microsoft.TridentWordAddIn.Server;

namespace Microsoft.TridentWordAddIn.Presentation
{
    public partial class WorkflowParametersUserControl : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowParametersUserControl"/> class.
        /// </summary>
        public WorkflowParametersUserControl()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Clears the parameters.
        /// </summary>
        public void ClearParameters()
        {
            this.lvInputParameters.Items.Clear();
            this.lvInputParameters.Groups.Clear();
            this.lvOutputParameters.Items.Clear();
            this.lvOutputParameters.Groups.Clear();
        }

        /// <summary>
        /// Updates the parameters.
        /// </summary>
        /// <param name="activities">The activities.</param>
        /// <param name="job">The job.</param>
        public void UpdateParameters(List<WorkflowActivity> activities, WorkflowJob job)
        {
            this.ClearParameters();

            foreach (WorkflowActivity activity in activities)
            {
                if (activity.Parameters.Count == 0)
                {
                    continue;
                }

                var inputParameter = from param in activity.Parameters
                                     where param.IsInput == true
                                     select param;

                var outputParameter = from param in activity.Parameters
                                      where param.IsInput == false
                                      select param;

                UpdateListView(job, inputParameter, activity, this.lvInputParameters);
                UpdateListView(job, outputParameter, activity, this.lvOutputParameters);
            }
        }

        /// <summary>
        /// Updates the list view.
        /// </summary>
        /// <param name="job">The job.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="activity">The activity.</param>
        /// <param name="listView">The list view.</param>
        private static void UpdateListView(WorkflowJob job, IEnumerable<WorkflowParameter> parameters,
                                           WorkflowActivity activity, ListView listView)
        {
            int index = 0;
            string value = string.Empty;
            JobParameter jobParam = null;
            ListViewItem item = null;
            ListViewGroup group = null;

            foreach (WorkflowParameter parameter in parameters)
            {
                if (index == 0)
                {
                    group = listView.AddGroup(activity.Label);
                }

                jobParam = job.GetParameter(activity.Name, parameter.Name);
                value = ParameterHelper.GetValue(parameter, jobParam);
                item = listView.AddItem(parameter.Name, new string[] { value });
                group.Items.Add(item);
                index++;
            }
        }
    }
}
